package com.jianggujin.zkgui;

import java.awt.BorderLayout;
import java.awt.Toolkit;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.zookeeper.CreateMode;

public class AddPathDialog extends JDialog {
    private JTextArea path;
    private JTextArea childPath;
    private JTextArea childValue;
    private JComboBox<CreateMode> nodeType;
    private MainFrame mainFrame;
    private JCheckBox setNull;
    private JButton saveBtn;
    private JButton close;

    private AddPathDialog(MainFrame parent, String path) {
        super(parent);
        this.mainFrame = parent;
        initComponent(path);
        initDialog();
    }

    private void initComponent(String path) {
        Box center = Box.createVerticalBox();
        center.add(Box.createVerticalStrut(10));

        Box tmp = Box.createHorizontalBox();
        tmp.add(Box.createHorizontalStrut(10));
        tmp.add(new JLabel("父节点："));
        tmp.add(Box.createHorizontalStrut(10));
        this.path = new JTextArea(path);
        this.path.setEditable(false);
        this.path.setLineWrap(true);
        this.path.setRows(2);
        tmp.add(new JScrollPane(this.path));
        tmp.add(Box.createHorizontalStrut(10));
        center.add(tmp);

        center.add(Box.createVerticalStrut(10));

        tmp = Box.createHorizontalBox();
        tmp.add(Box.createHorizontalStrut(10));
        tmp.add(new JLabel("子节点："));
        tmp.add(Box.createHorizontalStrut(10));
        childPath = new JTextArea();
        childPath.setRows(3);
        childPath.setLineWrap(true);
        tmp.add(new JScrollPane(childPath));
        tmp.add(Box.createHorizontalStrut(10));
        center.add(tmp);

        center.add(Box.createVerticalStrut(10));

        tmp = Box.createHorizontalBox();
        tmp.add(Box.createHorizontalStrut(10));
        tmp.add(new JLabel("类　型："));
        tmp.add(Box.createHorizontalStrut(10));
        nodeType = new JComboBox<CreateMode>();
        for (CreateMode item : CreateMode.values()) {
            nodeType.addItem(item);
        }
        tmp.add(nodeType);
        tmp.add(Box.createHorizontalStrut(10));
        center.add(tmp);

        center.add(Box.createVerticalStrut(10));

        tmp = Box.createHorizontalBox();
        tmp.add(Box.createHorizontalStrut(10));
        tmp.add(new JLabel("节点值："));
        tmp.add(Box.createHorizontalStrut(10));
        childValue = new JTextArea();
        childValue.setRows(3);
        childValue.setLineWrap(true);
        tmp.add(new JScrollPane(childValue));
        tmp.add(Box.createHorizontalStrut(10));
        center.add(tmp);

        this.add(center);

        JPanel buttom = new JPanel();
        setNull = new JCheckBox("设置NULL");
        setNull.addActionListener(e -> {
            childValue.setEditable(!setNull.isSelected());
        });
        buttom.add(setNull);
        saveBtn = new JButton("保存");
        saveBtn.addActionListener(e -> {
            String childPath = this.childPath.getText();
            if (childPath.length() == 0) {
                JOptionPane.showMessageDialog(AddPathDialog.this, "子节点必填");
                return;
            }
            this.mainFrame.addPath(this.path.getText(), childPath,
                    setNull.isSelected() ? null : this.childValue.getText(),
                    (CreateMode) this.nodeType.getSelectedItem());
            this.dispose();
        });
        close = new JButton("关闭");
        close.addActionListener(e -> {
            this.dispose();
        });
        buttom.add(saveBtn);
        buttom.add(close);
        this.add(buttom, BorderLayout.SOUTH);
    }

    private void initDialog() {
        setTitle("添加节点");
        setModal(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setSize(500, 300);
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        // 设置窗体在显示器居中显示
        this.setBounds((width - getWidth()) / 2, (height - getHeight()) / 2, getWidth(), getHeight());
        setResizable(false);
        this.setVisible(true);
    }

    public static void showDialog(MainFrame parent, String path) {
        new AddPathDialog(parent, path);
    }
}
